package org.example.StudentManagementSystem;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Scanner;

public class StudentManagementSystem02 {
    Scanner sc = new Scanner(System.in);
    private Collection students = new ArrayList();

    // 1.添加学生信息的通用方法
    public void addStudent(Student student) {
        if (student != null && validateStudentEmail(student)) {
            students.add(student);
        } else {
            System.out.println("学号：" + student.getStudentId() + "，姓名："
                    + student.getStudentName() + "，邮箱⭐："
                    + student.getMail() + "，不符合邮箱格式，添加失败");
        }
    }

    //校验邮箱方法
    public boolean validateStudentEmail(Student student) {
        if (student == null || student.getMail() == null) {
            return false;
        } else {
            return student.getMail().matches("\\w+@\\w+\\.\\w+");
        }
    }

    //2.展示学生信息：
    public void showAllStudents(Collection students) {
        int i = 1;
        for (Object student : students) {
            System.out.println((i++) + " " + student);
        }
    }

    public void showAllStudents() {
        showAllStudents(students);
    }

    //3.判断学生是否存在
    public boolean containsStudent(Student student) {
        return students.contains(student);
    }

    //4.根据学生学号查找某个学生
    public Student selectByStudentId(int studentID) {
        for (Object student : students) {
            if (student instanceof Student stu &&
                    stu.getStudentId() == studentID) {
                return stu;
            }
        }
        return null;
    }

    //5.求学生平均成绩
    public int getAvgrageScoreBySubject(String subject) {
        int totalScore = 0;
        for (Object student : students) {
            if (student instanceof Student stu) {
                int score = getScoreBySubject(stu, subject);
                totalScore += score;
            }
        }
        return students.size() == 0 ? 0 : totalScore / students.size();
    }

    public int getScoreBySubject(Student student, String subject) {
        switch (subject) {
            case "语文" -> {
                return student.getChinese();
            }
            case "数学" -> {
                return student.getMath();
            }
            case "物理" -> {
                if (student instanceof JuniorStudent juniorStudent) {
                    juniorStudent.getPhysics();
                }
            }
            case "历史" -> {
                if (student instanceof JuniorStudent juniorStudent) {
                    juniorStudent.getHistory();
                }
            }
        }
        return 0;
    }

    //6.判断指定日期之前的学生
    public Collection getStudentsEnrolledBefore(String date) {
        //创建一个容器专门用来储存符合条件的学生
        Collection temp = new ArrayList();
        for (Object student : students) {
            if (student instanceof Student stu &&
                    stu.getEnrollmentDate() != null) {
                if (stu.getEnrollmentDate().isBefore(LocalDate.parse(date))
                        || stu.getEnrollmentDate().isEqual(LocalDate.parse(date))) {
                    temp.add(student);
                }
            }
        }
        return temp;
    }
    //判断指定日期之后的学生
    public Collection getStudentsEnrolledAfter(String date) {
        //创建一个容器专门用来储存符合条件的学生
        Collection temp = new ArrayList();
        for (Object student : students) {
            if (student instanceof Student stu &&
                    stu.getEnrollmentDate() != null) {
                if (stu.getEnrollmentDate().isAfter(LocalDate.parse(date))
                        || stu.getEnrollmentDate().isEqual(LocalDate.parse(date))) {
                    temp.add(student);
                }
            }
        }
        return temp;
    }
}
